package com.peach.springbootdemo.demo.web.demo;

import com.peach.springbootdemo.demo.model.Demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author peacher
 * @date 2019-08-01
 */
@Controller
@RequestMapping("/page")
public class IndexController {

    /**
     * 启动后访问  http://127.0.0.1:8080/page/index
     * @param map
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request, ModelMap map) {
        map.addAttribute("message", "http://www.spring boot.com");
        map.addAttribute("name", "程序媛");
        map.addAttribute("userName", "程序员");
        map.addAttribute("url", "https://www.baidu.com/");
        map.addAttribute("sex", "woman");
        map.addAttribute("count", 12);
        map.addAttribute("date", new Date());

        request.setAttribute("request","i am request");
        request.getSession().setAttribute("session","i am session");

        return "demo/demoIndex";
    }


    /**
     * 启动后访问  http://127.0.0.1:8080/page/index
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public String list(ModelMap map) {
        map.addAttribute("demos", getDemoList());
        return "demo/demoLists";
    }


    private List<Demo> getDemoList(){
        List<Demo> list=new ArrayList<>();
        Demo demo1=new Demo();
        demo1.setCode("100");
        demo1.setName("演示1");
        demo1.setAge(100);

        Demo demo2=new Demo();
        demo2.setCode("200");
        demo2.setName("演示2");
        demo2.setAge(20);

        Demo demo3=new Demo();
        demo3.setCode("300");
        demo3.setName("演示3");
        demo3.setAge(30);

        list.add(demo1);
        list.add(demo2);
        list.add(demo3);
        return  list;
    }

    /**
     * 启动浏览器访问 http://127.0.0.1:8080/page/layout
     * @return
     */
    @RequestMapping("/layout")
    public String index() {
        return "index";
    }

    /**
     * 启动浏览器访问 http://127.0.0.1:8080/page/layout2
     * @return
     */
    @RequestMapping("/layout2")
    public String layout() {
        return "layout";
    }

    /**
     * 启动浏览器访问 http://127.0.0.1:8080/page/home
     * @return
     */
    @RequestMapping("/home")
    public String home() {
        return "home";
    }


    /**
     * 启动浏览器访问 http://127.0.0.1:8080/page/webSocket
     * @return
     */
    @RequestMapping("/webSocket")
    public String  webSocketIndex(){
        return "webSocket";
    }

}
